blog

Home / DeveloperSection / Blogs / Making Custom Error Page in Mvc4

Making Custom Error Page in Mvc4

Manish Kumar1652 18-Feb-2017

In this section I will explain about making of custom error page. We have seen in most of the cases default error page occur. Now we will make our custom error. We can design it according to our need.

Below are the few steps for creating our error page.

First of all add shared folder in the view folder 

Making Custom Error Page in Mvc4

In the shared folder add a view for error view. We can design as want. I had make a simple error page.

 

@{
    Layout = null;
}
 
<!DOCTYPEhtml>
 
<html>
<head>
    <metaname="viewport"content="width=device-width"/>
    <title>error</title>
</head>
<body>
    <div>
        <h2>Sorry !!! this view is not available</h2>
    </div>
</body>
</html>


  Now add a Home controller and add following code.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MvcApplication6.Controllers
{
    
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
       [HandleError]
        publicActionResult Index()
        {
          
            return View();
        }
        publicActionResult Add()
        {
            return View();
        }
 
    }
}
 
It is set for particular method we can set it all the method for this do like this.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace MvcApplication6.Controllers
{
     [HandleError]
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
      
        publicActionResult Index()
        {
          
            return View();
        }
        publicActionResult Add()
        {
            return View();
        }
 
    }
}

 

In the home controller add two method Index() and Add() . HandleError attribute render error page in share folder when error occur and one more important thing is to set customError mode on of web. config root. In System.Web section. There are two web.config in the application in the view folder and in the root .we have to change in the root web.config.


Updated 17-Mar-2018

Leave Comment

Comments

Liked By